/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
\file Help.qml
\brief This is the help view of the SmashMiner game.

See the \c helpView at the main.qml view.

\sa main.qml
*/

import Qt 4.7

Rectangle {
    id: helpView


    // Signal to return main view
    signal back()

    // Vertical margins of the view area
    property int marginsVertical: height/20
    // Height of the view header
    property int headerHeight: marginsVertical*2
    // Horizontal margins of the view area
    property int marginsHorizontal: headerHeight+marginsVertical
    // Height of the buttons used at this view
    property int buttonHeight: headerHeight*0.9

    // Is the right arrow button currently visible
    property bool rightArrowButtonVisible: true
    // Is the left arrow button currently visible
    property bool leftArrowButtonVisible: false

    width: parent.width
    height: parent.height
    color: "black"
    z: -10
    visible: true

    Image {
        id: background
        anchors.fill: parent
        source: "qrc:/images/1024x512_01.jpg"
    }

    ViewHeader {
        id: helpHeader
        heading: qsTr("HELP")
    }

    Rectangle {
        id: helpPageContainer

        width: parent.width

        anchors {
            top: helpHeader.bottom;
            bottom: helpFooter.top;
            left: parent.left;
            right: parent.right;
            leftMargin: marginsHorizontal; 
            rightMargin: marginsHorizontal;
        }
        color: "black"
        border { width: 5; color: "#333333" }

        Item {
            id: helpPages

            // Page of this view that is currently visible
            property int currentPage: 1

            // 1) Set number of help pages here
            // 2) Add a state for each page
            // 3) Add the new help page inside Item element after the last help page
            property int numberOfPages: 5

            // Move to next page of this view
            function nextPage() {
                if (currentPage < numberOfPages) {
                    soundEngine.playMenuSound();
                    currentPage++;
                }
            }

            // Move to previous page of this view
            function previousPage() {
                if (currentPage > 1) {
                    soundEngine.playMenuSound();
                    currentPage--;
                }
            }

            // Get the offset of the x value for the currentPage
            function pageOffset() {
                return -((currentPage-1) * (helpPageContainer.width + marginsHorizontal));
            }

            // Slot for currentPageChanged() signal emitted
            // from the change of the property currentPage
            onCurrentPageChanged: {
                helpPages.state = 'page' + currentPage;
                rightArrowButtonVisible = false;
                leftArrowButtonVisible = false;
                pageChangeTimer.start();
            }

            width: parent.width * numberOfPages 
            height: parent.height
            anchors.top: parent.top;
            x: 0; // x property will be changed when switching pages

            // Animate property x change when changing the view pages
            transitions: [
                 Transition {
                      NumberAnimation { properties: "x"; duration: 500 }
                 }
            ]

            // States for pages -> different states change the x value and NumberAnimation is started.
            states: [
                State {
                    name: "page1"
                    PropertyChanges {
                        target: helpPages;
                        x: pageOffset(); 
                    }
                },

                State {
                    name: "page2"
                    PropertyChanges {
                        target: helpPages;
                        x: pageOffset(); 
                    }
                },

                State {
                    name: "page3"
                    PropertyChanges {
                        target: helpPages;
                        x: pageOffset(); 
                    }
                },

                State {
                    name: "page4"
                    PropertyChanges {
                        target: helpPages;
                        x: pageOffset(); 
                    }
                },

                State {
                    name: "page5"
                    PropertyChanges {
                        target: helpPages;
                        x: pageOffset(); 
                    }
                }
            ]

            // Help pages start >>

            HelpPage1 {
                width: helpPageContainer.width
                height: helpPageContainer.height
                x: 0
            }

            HelpPage2 {
                width: helpPageContainer.width
                height: helpPageContainer.height
                x: helpPageContainer.width + marginsHorizontal
            }

            HelpPage3 {
                width: helpPageContainer.width
                height: helpPageContainer.height
                x: 2 * (helpPageContainer.width + marginsHorizontal)
            }

            HelpPage4 {
                width: helpPageContainer.width
                height: helpPageContainer.height
                x: 3 * (helpPageContainer.width + marginsHorizontal)
            }

            HelpPage5 {
                width: helpPageContainer.width
                height: helpPageContainer.height
                x: 4 * (helpPageContainer.width + marginsHorizontal)
            }

            // << Help pages end here

            // Page change timer for showing and hiding the ArrowButtons
            Timer {
                id: pageChangeTimer
                interval: 500
                onTriggered: {
                    if (helpPages.currentPage == 1) {
                        rightArrowButtonVisible = true;
                    } else if (helpPages.currentPage < helpPages.numberOfPages) {
                        leftArrowButtonVisible = true;
                        rightArrowButtonVisible = true;
                    } else if (helpPages.currentPage == helpPages.numberOfPages) {
                        leftArrowButtonVisible = true;
                    }
                }
            } // pageChangeTimer

            // Sweep area for changing view pages by sweeping the screen
            MouseArea {
                id: sweepArea

                property int startX: -1
                property int minSweepLength: parent.width*0.1

                anchors.fill: parent
                focus: true
                onPressed: {
                    // When the screen is touched store the start point
                    startX = mouseX;
                }
                onReleased: {
                    // When released calculate the lenght of the sweep
                    var length = mouseX - startX;

                    if (length > minSweepLength) {
                        // Sweeped left to right
                        helpPages.previousPage();
                    } else if (length < -minSweepLength) {
                        // Sweeped right to left
                        helpPages.nextPage();
                    }
                    startX = -1;
                }
            } // sweepArea
        } // helpPages

        ArrowButton {
            id: leftArrowButton
            width: buttonHeight*1.5
            height: buttonHeight*1.5
            visible:  leftArrowButtonVisible ? "true" : "false"
            rotation:  180
            anchors { right: helpPageContainer.left; verticalCenter: helpPageContainer.verticalCenter }
            onClicked: helpPages.previousPage()
        }

        ArrowButton {
            id: rightArrowButton
            width: buttonHeight*1.5
            height: buttonHeight*1.5
            visible: rightArrowButtonVisible ? "true" : "false"
            anchors { left: helpPageContainer.right; verticalCenter: helpPageContainer.verticalCenter }
            onClicked: helpPages.nextPage()
        }
    } // helpPageContainer

    ViewFooter {
        id: helpFooter
        width: parent.width
        height: headerHeight

        onBackClicked: {
            soundEngine.playMenuSound();
            back();
            if (helpPages.currentPage != 1) {
                helpPages.currentPage = 1;
            }
        }
    } // helpFooter
}
